InterfaceDemo.py

Enumerate and Control Frame Grabber(s)

It shows how to enumerate one or more frame grabbers via enumeration APIs and perform operations such as turning on frame grabbers and setting attributes.

1 # -- coding: utf-8 --
2 
3 import sys
4 import threading
5 import platform
6 import os
7 from ctypes import *
8 
9 
10 # Compatible with different operating systems to load DDL
11 currentsystem = platform.system()
12 if currentsystem == 'Windows':
13  sys.path.append(os.path.join(os.getenv('MVCAM_COMMON_RUNENV'), "Samples", "Python", "MvImport"))
14 else:
15  sys.path.append(os.path.join("..", "..", "MvImport"))
16 
17 from MvCameraControl_class import *
18 
19 
20 # Compatible with input processing of Python 2.X and 3.X
21 if sys.version_info[0] < 3:
22  # Python 2.x
23  input_func = raw_input
24 else:
25  # Python 3.x
26  input_func = input
27 
28 # Decoding Characters
29 def decoding_char(ctypes_char_array):
30  """
31  Safely decode a string from a ctypes character array.
32  Compatible with Python 2.x and 3.x, as well as 32-bit and 64-bit environments.
33  """
34  byte_str = memoryview(ctypes_char_array).tobytes()
35 
36  # Truncate at the first null character
37  null_index = byte_str.find(b'\x00')
38  if null_index != -1:
39  byte_str = byte_str[:null_index]
40 
41  # Attempt to decode using multiple encodings
42  for encoding in ['gbk', 'utf-8', 'latin-1']:
43  try:
44  return byte_str.decode(encoding)
45  except UnicodeDecodeError:
46  continue
47 
48  # If all encodings fail, use a replacement strategy
49  return byte_str.decode('latin-1', errors='replace')
50 
51 
52 if __name__ == "__main__":
53 
54  try:
55  # Initialize SDK resources
56  MvCamera.MV_CC_Initialize()
57 
58  SDKVersion = MvCamera.MV_CC_GetSDKVersion()
59  print ("SDKVersion[0x%x]" % SDKVersion)
60 
61  interfaceList = MV_INTERFACE_INFO_LIST()
62  transportLayerType = MV_GIGE_INTERFACE | MV_CAMERALINK_INTERFACE | MV_CXP_INTERFACE | MV_XOF_INTERFACE
63 
64  # Enumerate frame grabbers
65  ret = MvCamera.MV_CC_EnumInterfaces(transportLayerType, interfaceList)
66  if ret != 0:
67  print("enum interfaces fail! ret[0x%x]" % ret)
68  sys.exit()
69 
70  if interfaceList.nInterfaceNum == 0:
71  print("find no interface!")
72  sys.exit()
73 
74  print("Find %d interfaces!" % interfaceList.nInterfaceNum)
75 
76  for i in range(0, interfaceList.nInterfaceNum):
77  interfaceInfo = cast(interfaceList.pInterfaceInfos[i], POINTER(MV_INTERFACE_INFO)).contents
78  print("interface: [%d]" % i)
79 
80  displayName = decoding_char(interfaceInfo.chDisplayName)
81  print("display name: %s" % displayName)
82 
83  serialNumber = decoding_char(interfaceInfo.chSerialNumber)
84  print("serial number: %s" % serialNumber)
85 
86  modelName = decoding_char(interfaceInfo.chModelName)
87  print("model name: %s" % modelName)
88 
89  interfaceId = decoding_char(interfaceInfo.chInterfaceID)
90  print("interface id: %s" % interfaceId)
91 
92  nConnectionNum = input_func("please input the number of the interface to connect:")
93 
94  if int(nConnectionNum) >= interfaceList.nInterfaceNum:
95  print("input error!")
96  sys.exit()
97 
98  # Create a camera instance
99  cam = MvCamera()
100 
101  # Select the frame grabber, and create a handle
102  curInterface = cast(interfaceList.pInterfaceInfos[int(nConnectionNum)], POINTER(MV_INTERFACE_INFO)).contents
103 
104  ret = cam.MV_CC_CreateInterface(curInterface)
105  if ret != 0:
106  raise Exception("create interface handle fail! ret[0x%x]" % ret)
107  # Turn on the device
108  ret = cam.MV_CC_OpenInterface()
109  if ret != 0:
110  raise Exception("open interface fail! ret[0x%x]" % ret)
111  else:
112  print("open interface success")
113 
114  # Get features
115  stEnumValue = MVCC_ENUMVALUE()
116  ret =cam.MV_CC_GetEnumValue("StreamSelector", stEnumValue)
117  if ret != 0:
118  raise Exception("get StreamSelector fail! ret[0x%x]" % ret)
119 
120  # Set features
121  ret = cam.MV_CC_SetEnumValue("StreamSelector", stEnumValue.nCurValue)
122  if ret != 0:
123  raise Exception("set StreamSelector fail! ret[0x%x]" % ret)
124  else:
125  print("set StreamSelector [%d] success" % stEnumValue.nCurValue)
126 
127  # Turn off the frame grabber
128  ret = cam.MV_CC_CloseInterface()
129  if ret != 0:
130  raise Exception("close interface fail! ret[0x%x]" % ret)
131  else:
132  print("close interface success")
133 
134  # Destroy frame grabber handle
135  cam.MV_CC_DestroyInterface()
136 
137  except Exception as e:
138  print(e)
139  cam.MV_CC_CloseDevice()
140  cam.MV_CC_DestroyInterface()
141  finally:
142  # Release SDK resources
143  MvCamera.MV_CC_Finalize()